//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.platform.service;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.base.domain.SystemClassUserImport;
import com.ruoyi.live.base.domain.SystemClassUserImportDetail;
import com.ruoyi.live.base.domain.User;
import com.ruoyi.live.base.mapper.SystemClassUserImportDetailMapper;
import com.ruoyi.live.base.mapper.SystemClassUserImportMapper;
import com.ruoyi.live.base.mapper.UserMapper;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.commons.IConvert;
import com.ruoyi.live.tool.commons.IMD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class SystemClassUserImportService {
    private static final Logger log = LoggerFactory.getLogger(SystemClassUserImportService.class);
    @Autowired
    SystemClassUserImportMapper dao;
    @Autowired
    UserMapper userDao;
    @Autowired
    SystemClassUserImportDetailMapper detailDao;

    public SystemClassUserImportService() {
    }

    public void addSystemClassUserImport(SystemClassUserImport entity, MultipartFile file, HttpServletRequest request) throws Exception {
        try {
            entity.setCreateTime(System.currentTimeMillis());
            entity.setCreateUser(SessionInfo.getUserId());
            entity.setState(1);
            this.dao.insert(entity);
            if (file != null) {
                this.doUploadFile(file, request, entity);
            }

        } catch (Exception var5) {
            var5.printStackTrace();
            log.error("新增SystemClassUserImport失败,编号SystemClassUserImportService-addSystemClassUserImport-1");
            throw new Exception("新增失败,编号SystemClassUserImportService-addSystemClassUserImport-1");
        }
    }

    public void doUploadFile(MultipartFile file, HttpServletRequest request, SystemClassUserImport entity) throws Exception {
        try {
            List<Map<Integer, String>> objects = EasyExcel.read(new BufferedInputStream(file.getInputStream())).sheet().doReadSync();

            int successCount = 0;
            int errorCount = 0;

            for(int i = 0; i < objects.size(); ++i) {
                Map<Integer, String> model = (Map)objects.get(i);
                String userLoginName = (String)model.get(0);
                String userRealName = (String)model.get(2);
                String password = (String)model.get(1);
                String cellphone = (String)model.get(3);
                if (ICommons.isNullOrEmpty(password)) {
                    password = "123456";
                }

                SystemClassUserImportDetail detail = (new SystemClassUserImportDetail()).setImportId(entity.getId()).setUserCellphone(cellphone).setUserLoginName(userLoginName).setUserPassword(password).setUserRealName(userRealName);

                try {
                    User user = new User();
                    user.setClassId(entity.getClassId());
                    user.setUserCellphone(cellphone);
                    user.setUserCreateTime(System.currentTimeMillis());
                    user.setUserCreateUserId(SessionInfo.getUserId());
                    user.setUserLoginName(userLoginName);
                    user.setUserPasswordMd5(IMD5.getMD5(password));
                    user.setUserLoginNameMd5(IMD5.getMD5(userLoginName));
                    user.setUserRealName(userRealName);
                    user.setUserState(1);
                    user.setUserType(3);
                    user.setUserHeaderImg("pic_tea.png");
                    user.setImportId(entity.getId());
                    this.userDao.insert(user);
                    detail.setState(1);
                    ++successCount;
                } catch (Exception var15) {
                    var15.printStackTrace();
                    detail.setState(2);
                    detail.setBecause("用户名已存在");
                    ++errorCount;
                }

                this.detailDao.insert(detail);
            }

            if (errorCount != 0) {
                entity.setState(2);
            }

            entity.setSuccessCount(successCount);
            entity.setErrorCount(errorCount);
            this.dao.updateById(entity);
        } catch (Exception var16) {
            var16.printStackTrace();
            throw var16;
        }
    }

    public void updateSystemClassUserImport(SystemClassUserImport entity) throws Exception {
        try {
            this.dao.updateById(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("更新SystemClassUserImport失败,编号SystemClassUserImportService-updateSystemClassUserImport-1,id:{}", entity.getId());
            throw new Exception("更新失败,编号SystemClassUserImportService-updateSystemClassUserImport-1");
        }
    }

    public SystemClassUserImport getSystemClassUserImport(Long id) {
        return (SystemClassUserImport)this.dao.selectById(id);
    }

    public int deleteSystemClassUserImport(String[] ids) throws Exception {
        if (ids.length == 0) {
            throw new Exception("请选择要删除的数据,编号SystemClassUserImportService-deleteSystemClassUserImport-1");
        } else {
            try {
                int count = 0;

                for(int i = 0; i < ids.length; ++i) {
                    if (!ICommons.isNullOrEmpty(ids[i])) {
                        count += this.dao.deleteById(IConvert.string2long(ids[i]));
                    }
                }

                return count;
            } catch (Exception var4) {
                var4.printStackTrace();
                log.error("删除SystemClassUserImport失败,编号SystemClassUserImportService-deleteSystemClassUserImport-2,ids:{}", ids.toString());
                throw new Exception("删除的数据失败,编号SystemClassUserImportService-deleteSystemClassUserImport-2");
            }
        }
    }

    public List<SystemClassUserImport> query(SystemClassUserImport entity) {
        if (entity.getClassId() == null) {
            return null;
        } else {
            QueryWrapper<SystemClassUserImport> wrapper = (QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)(new QueryWrapper()).eq(!ICommons.isNullOrEmpty(entity.getId()), "id", entity.getId())).eq("class_id", entity.getClassId())).eq("create_user", SessionInfo.getUserId())).like(!ICommons.isNullOrEmpty(entity.getTitle()), "title", entity.getTitle())).eq(!ICommons.isNullOrEmpty(entity.getCreateTime()), "create_time", entity.getCreateTime())).eq(!ICommons.isNullOrEmpty(entity.getState()), "state", entity.getState())).like(!ICommons.isNullOrEmpty(entity.getStoreName()), "store_name", entity.getStoreName())).eq(!ICommons.isNullOrEmpty(entity.getSuccessCount()), "success_count", entity.getSuccessCount())).eq(!ICommons.isNullOrEmpty(entity.getErrorCount()), "error_count", entity.getErrorCount());
            return this.dao.selectList(wrapper);
        }
    }

    public void doReImport(Long id) {
        SystemClassUserImportDetail detail = (SystemClassUserImportDetail)this.detailDao.selectById(id);
        SystemClassUserImport impor = (SystemClassUserImport)this.dao.selectById(detail.getImportId());
        this.doImportSingle(detail, impor.getClassId(), impor.getId());
        this.doCount(impor.getId());
    }

    private void doCount(Long id) {
        Long correct = this.detailDao.selectCount(new QueryWrapper((new SystemClassUserImportDetail()).setImportId(id).setState(1)));
        Long error = this.detailDao.selectCount(new QueryWrapper((new SystemClassUserImportDetail()).setImportId(id).setState(2)));
        this.dao.updateById((new SystemClassUserImport()).setId(id).setSuccessCount(correct.intValue()).setErrorCount(error.intValue()));
    }

    private void doImportSingle(SystemClassUserImportDetail detail, Long classId, Long importId) {
        try {
            User user = new User();
            user.setClassId(classId);
            user.setUserCellphone(detail.getUserCellphone());
            user.setUserCreateTime(System.currentTimeMillis());
            user.setUserCreateUserId(SessionInfo.getUserId());
            user.setUserLoginName(detail.getUserLoginName());
            user.setUserPasswordMd5(IMD5.getMD5(detail.getUserPassword()));
            user.setUserLoginNameMd5(IMD5.getMD5(detail.getUserLoginName()));
            user.setUserRealName(detail.getUserRealName());
            user.setUserState(1);
            user.setUserType(3);
            user.setImportId(importId);
            this.userDao.insert(user);
            this.detailDao.updateById((new SystemClassUserImportDetail()).setId(detail.getId()).setState(1).setBecause(""));
        } catch (Exception var5) {
            var5.printStackTrace();
            throw var5;
        }
    }

    public void importAll(Long id) {
        SystemClassUserImport impor = (SystemClassUserImport)this.dao.selectById(id);
        List<SystemClassUserImportDetail> details = this.detailDao.selectList(new QueryWrapper((new SystemClassUserImportDetail()).setImportId(id).setState(2)));
        Iterator var4 = details.iterator();

        while(var4.hasNext()) {
            SystemClassUserImportDetail detail = (SystemClassUserImportDetail)var4.next();
            this.doImportSingle(detail, impor.getClassId(), id);
        }

        this.doCount(id);
    }

    public List<SystemClassUserImport> queryList(Long id) {
        return this.dao.selectList(new QueryWrapper((new SystemClassUserImport()).setClassId(id)));
    }
}
